package com.visual.luckydemo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class ChannelBroadcastActivity extends Activity implements View.OnClickListener {
    private static final String TAG = ChannelBroadcastActivity.class.getSimpleName();

    private EditText etFromICast;
    private EditText etMyself;
    private String sender;
    private String cmd;
    private String param;
    private String channelJson;

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (ConstUtil.HOST.equals(intent.getAction())) {
                LogUtil.Logd(TAG, "onReceive is in");
                //解析出sender cmd 和 param参数,以String格式保存起来X
                sender = intent.getStringExtra(ConstUtil.SENDER);
                cmd = intent.getStringExtra(ConstUtil.CMD);
                param = intent.getStringExtra(ConstUtil.PARAM);

                if (ConstUtil.ICAST.equals(sender))//海尔项目的的请求处理
                {
                    if (ConstUtil.GET_ICAST_CHANNEL_LIST.equals(cmd))//获取频道列表
                    {
                        LogUtil.Logd(TAG, "获取频道列表 param = " + param);
                        channelJson = param;
                        try {
                            JSONArray jsonObject = new JSONArray(param);
                            String jsonStr = jsonObject.toString(4);
                            LogUtil.Logd(TAG, "获取频道列表 JSON = " + jsonStr);
                            etFromICast.setText(jsonStr);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        return;
                    }
                }
            }
        }
    };
    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel_broadcast);
        findViewById(R.id.btn_get_channels).setOnClickListener(this);
        findViewById(R.id.btn_send_channels).setOnClickListener(this);
        etFromICast = (EditText) findViewById(R.id.et_channels_from_icast);
        etMyself = (EditText) findViewById(R.id.et_channels_myself);
        try {
            JSONArray jsonArray = new JSONArray(getFromAssets("my_channels"));
            etMyself.setText(jsonArray.toString(4));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        initBroadCastReceiver();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_channel_broadcast, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    void initBroadCastReceiver() {

        IntentFilter filter = new IntentFilter(ConstUtil.HOST);
        filter.addAction(ConstUtil.GET_ICAST_CHANNEL_LIST);
        registerReceiver(receiver, filter);
    }

    @Override
    public void onClick(View v) {
        if (R.id.btn_get_channels == v.getId()) {
            Intent intent = new Intent(ConstUtil.CLIENT);
            intent.putExtra(ConstUtil.SENDER, ConstUtil.HAIER);
            intent.putExtra(ConstUtil.CMD, ConstUtil.GET_ICAST_CHANNEL_LIST);
            this.sendBroadcast(intent);
            Toast.makeText(this, "获取频道广播发送完成", Toast.LENGTH_SHORT).show();
        }

        if (R.id.btn_send_channels == v.getId()) {
            channelJson = etMyself.getText().toString();
            Intent intent = new Intent(ConstUtil.CLIENT);
            intent.putExtra(ConstUtil.SENDER, ConstUtil.HAIER);
            intent.putExtra(ConstUtil.CMD, ConstUtil.SET_ICAST_CHANNEL_LIST);
            intent.putExtra(ConstUtil.PARAM, channelJson);
            this.sendBroadcast(intent);
            Toast.makeText(this, "设置频道广播发送完成", Toast.LENGTH_SHORT).show();
        }
    }

    public String getFromAssets(String fileName) {
        try {
            InputStreamReader inputReader = new InputStreamReader(getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            while ((line = bufReader.readLine()) != null)
                Result += line;
            return Result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

//    public String getFromRaw(){
//        try {
//            InputStreamReader inputReader = new InputStreamReader( getResources().openRawResource(R.raw.test1));
//            BufferedReader bufReader = new BufferedReader(inputReader);
//            String line="";
//            String Result="";
//            while((line = bufReader.readLine()) != null)
//                Result += line;
//            return Result;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
